home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Development
/
Source
/
MacStarter (THINK C 5.0⁄6.0)
/
globals-MacStarter.c
< prev
next >
Wrap
Text File
|
1994-01-25
|
15KB
|
592 lines
/* MacStarter is a shell for simple Macintosh applications shell written in
THINK C, with some small use of its object-oriented features.
The whole system is described in a README file tha should be in
the same folder with this file. There are no comments on this file.
You should not need to know the implementation of any of the functions
written here. Information on what is available from this file for
you to use elsewhere in your program is in the README file.
*/
#include "globals-MacStarter.h"
#include "stdlib.h"
EventRecord gEvent;
int gClickCount = 0;
long lastClickTime = 0;
Point lastClickPt = { 0,0 };
Rect gWindowRect = { 0,0,0,0 };
xWindow *xWindowList = 0;
int xWindow::Window2XWindow(WindowPtr win, xWindow** xWin) {
xWindow *run;
run = xWindowList;
while ( run && run->theWindow != win )
run = run->nextWindow;
*xWin = run;
return (run != 0);
}
void xWindow::SetDefaults(void) {
features = hasHScroll + hasVScroll + hasGoAway + hasZoom + hasGrow;
topScrollOffset = 0;
bottomScrollOffset = 0;
leftScrollOffset = 0;
rightScrollOffset = 0;
minH = 50;
minV = 50;
maxH = 32000;
maxV = 32000;
hLinesPerPage = 1;
vLinesPerPage = 1;
}
void xWindow::OpenInRect(Str255 title, int left, int top, int right, int bottom) {
int height,width;
height = screenBits.bounds.bottom - screenBits.bounds.top - 20;
width = screenBits.bounds.right - screenBits.bounds.left;
if ( right - left <= 0 || right - left > width) {
left = 2;
right = screenBits.bounds.right - 2;
};
if ( bottom - top <= 0 || bottom - top > height) {
top = 40;
bottom = screenBits.bounds.bottom - 2;
};
SetDefaults();
doBasicOpen(title,left,top,right,bottom);
}
void xWindow::OpenFullScreen(Str255 title) {
Rect R;
R = screenBits.bounds;
R.top = 36;
InsetRect(&R,4,4);
OpenInRect(title,R.left,R.top,R.right,R.bottom);
}
void xWindow::Open(Str255 title) {
if (gWindowRect.bottom <= gWindowRect.top) {
gWindowRect = screenBits.bounds;
gWindowRect.top += 42;
gWindowRect.left += 4;
gWindowRect.right = gWindowRect.left + 3*(gWindowRect.right-gWindowRect.left)/4;
gWindowRect.bottom = gWindowRect.top + 3*(gWindowRect.bottom-gWindowRect.top)/4;
};
if (gWindowRect.bottom > screenBits.bounds.bottom)
OffsetRect(&gWindowRect,0,40 - gWindowRect.top);
if (gWindowRect.right > screenBits.bounds.right)
OffsetRect(&gWindowRect,4 - gWindowRect.left, 0);
OpenInRect(title,gWindowRect.left,gWindowRect.top,gWindowRect.right,gWindowRect.bottom);
OffsetRect(&gWindowRect,20,20);
}
void xWindow::doBasicOpen(Str255 title, int left, int top, int right, int bottom) {
Rect bounds;
short procID;
WindowPtr win;
nextWindow = xWindowList;
xWindowList = this;
SetRect(&bounds,left,top,right,bottom);
if (features & hasGrow)
procID = documentProc;
else
procID = noGrowDocProc;
if (features & hasZoom)
procID += 8;
theWindow = NewWindow( (Ptr) 0, &bounds, title, true, procID, (WindowPtr) -1, features & hasGoAway, 0);
win = theWindow;
if (features & hasHScroll) {
SetRect(&bounds,0,0,1,1);
hScroll = NewControl(win,&bounds,"\p",0,0,0,0,scrollBarProc,(long) this);
};
if (features & hasVScroll) {
SetRect(&bounds,0,0,1,1);
vScroll = NewControl(win,&bounds,"\p",0,0,0,0,scrollBarProc,(long) this);
};
adjustToNewSize();
}
xWindow::~xWindow(void) {
}
short xWindow::Close(void) {
xWindow *run;
run = xWindowList;
if (run == this) {
xWindowList = xWindowList->nextWindow;
} else {
while ( run && run->nextWindow != this ) {
run = run->nextWindow;
};
if (run) {
run->nextWindow = run->nextWindow->nextWindow;
};
};
DisposeWindow(theWindow);
delete this;
return 1;
}
void xWindow::doContentClick(Point localPt) {
/* Default action is to do nothing */
}
void xWindow::doKey(char ch) {
/* Default action is to do nothing */
}
void xWindow::doHScroll(int dh) {
/* Default action is to erase and redraw */
GrafPtr savePort;
Rect badRect;
GetPort(&savePort);
SetPort(theWindow);
badRect = theWindow->portRect;
if ( features & hasHScroll )
badRect.bottom -= 15;
if ( features & hasVScroll )
badRect.right -= 15;
EraseRect(&badRect);
doRedraw(&badRect);
SetPort(savePort);
}
void xWindow::doVScroll(int dv) {
/* Default action is to erase and redraw */
GrafPtr savePort;
Rect badRect;
GetPort(&savePort);
SetPort(theWindow);
badRect = theWindow->portRect;
if ( features & hasHScroll )
badRect.bottom -= 15;
if ( features & hasVScroll )
badRect.right -= 15;
EraseRect(&badRect);
doRedraw(&badRect);
SetPort(savePort);
}
void xWindow::doRedraw(Rect* badRect) {
/* default method does nothing */
}
void xWindow::doEvent(void) {
GrafPtr savePort;
switch (gEvent.what) {
case autoKey:
case keyDown: {
GetPort(&savePort);
SetPort(theWindow);
doKey(gEvent.message & 0xFF);
SetPort(savePort);
break;
}
case mouseDown: {
doClick(gEvent.where);
break;
}
case activateEvt: {
doActivate(gEvent.modifiers & activeFlag);
break;
}
case updateEvt: {
doUpdate();
}
}
}
/* Global Data for communication of doClick with ContinuousScroll */
static xWindow *scrollingWin;
pascal void xWindow::ContinuousScroll(ControlHandle theControl, short partNum) {
short change;
short oldVal, max;
short scrollLinesPerPage;
short scrollingHorizontally;
if ( theControl == scrollingWin->hScroll ) {
scrollingHorizontally = 1;
scrollLinesPerPage = scrollingWin->hLinesPerPage;
}
else {
scrollingHorizontally = 0;
scrollLinesPerPage = scrollingWin->vLinesPerPage;
};
oldVal = GetCtlValue(theControl);
max = GetCtlMax(theControl);
change = 0;
if (partNum == inDownButton) {
if (oldVal < max)
change = 1;
}
else if (partNum == inUpButton) {
if (oldVal > 0)
change = -1;
}
else if (partNum == inPageDown) {
if (oldVal + scrollLinesPerPage <= max)
change = scrollLinesPerPage;
else if (oldVal < max)
change = max - oldVal;
}
else if (partNum == inPageUp) {
if (oldVal - scrollLinesPerPage >0)
change = -scrollLinesPerPage;
else if (oldVal > 0)
change = -oldVal;
};
if (change) {
SetCtlValue(theControl,oldVal + change);
if (scrollingHorizontally)
scrollingWin->doHScroll(change);
else
scrollingWin->doVScroll(change);
}
}
void xWindow::doClick(Point globalPt) {
short partNum;
GrafPtr savePort;
WindowPtr win;
Point localPt;
short part;
short oldVal;
ControlHandle theControl;
partNum = FindWindow(globalPt,&win);
if (theWindow != FrontWindow() && partNum != inDrag) {
SelectWindow(theWindow);
lastClickTime = 0;
return;
};
if (TickCount() - lastClickTime <= GetDblTime()
&& abs(globalPt.h - lastClickPt.h) <= 3
&& abs(globalPt.v - lastClickPt.v) <= 3 )
gClickCount++;
else
gClickCount = 0;
lastClickPt = globalPt;
lastClickTime = TickCount();
switch (partNum) {
case inGrow: {
doGrow(globalPt);
break;
}
case inGoAway: {
if (TrackGoAway(theWindow,globalPt))
Close();
break;
}
case inDrag: {
doDrag(globalPt);
break;
}
case inZoomIn: {
doZoom(partNum);
break;
}
case inZoomOut: {
doZoom(partNum);
break;
}
case inContent: {
GetPort(&savePort);
SetPort(theWindow);
localPt = globalPt;
GlobalToLocal(&localPt);
if ( (localPt.v > theWindow->portRect.bottom - 15 && (features & hasHScroll))
|| (localPt.h > theWindow->portRect.right - 15 && (features & hasVScroll)) ) {
if ( (part = FindControl(localPt,theWindow,&theControl))
&& ( theControl == hScroll || theControl == vScroll ) ) {
if (part == inThumb) {
oldVal = GetCtlValue(theControl);
if (TrackControl(theControl,localPt,0L)) {
if (theControl == hScroll)
doHScroll(GetCtlValue(theControl) - oldVal);
else
doVScroll(GetCtlValue(theControl) - oldVal);
}
}
else {
scrollingWin = this;
TrackControl(theControl,localPt,&xWindow::ContinuousScroll);
}
}
}
else {
doContentClick(localPt);
};
SetPort(savePort);
}
}
}
void xWindow::doDrag(Point globalPt) {
Rect dragRect;
WindowPtr win;
dragRect = screenBits.bounds;
InsetRect(&dragRect,4,4);
dragRect.top -= 15;
win = theWindow;
DragWindow(win,globalPt,&dragRect);
}
void xWindow::doZoom(short partNum) {
Rect R;
WindowPtr win;
GrafPtr savePort;
R = theWindow->portRect;
win = theWindow;
GetPort(&savePort);
SetPort(win);
EraseRect(&R);
ZoomWindow(win,partNum,0);
InvalRect(&(win->portRect));
if ( features & hasHScroll )
HideControl(hScroll);
if ( features & hasVScroll )
HideControl(vScroll);
SetPort(savePort);
adjustToNewSize();
}
void xWindow::doGrow(Point globalPt) {
Rect sizeRect;
WindowPtr win;
GrafPtr savePort;
short height,width;
long newSize;
if ( minH < 1 || maxH <= minH ) {
minH = 50;
maxH = 32000;
};
if ( minV < 1 || maxV <= minV ) {
minV = 50;
maxV = 32000;
};
SetRect(&sizeRect,minH,minV,maxH,maxV);
win = theWindow;
if ( newSize = GrowWindow(theWindow,globalPt,&sizeRect) ) {
height = HiWord(newSize);
width = LoWord(newSize);
GetPort(&savePort);
SetPort(win);
EraseRect(&(win->portRect));
SizeWindow(win,width,height,0);
InvalRect(&(win->portRect));
if ( features & hasHScroll )
HideControl(hScroll);
if ( features & hasVScroll )
HideControl(vScroll);
SetPort(savePort);
adjustToNewSize();
}
}
void xWindow::doUpdate(void) {
GrafPtr savePort;
WindowPtr win;
Rect badRect;
Point savePen;
long saveColor;
win = theWindow;
GetPort(&savePort);
SetPort(win);
BeginUpdate(win);
EraseRect(&(win->portRect));
DrawControls(win);
if ( features & hasGrow ) {
DrawGrowIcon(win);
savePen = theWindow->pnSize;
saveColor = theWindow->fgColor;
PenSize(1,1);
ForeColor(whiteColor);
if ( !(features & hasVScroll) ) {
MoveTo(win->portRect.right - 15, 0);
LineTo(win->portRect.right - 15, win->portRect.bottom - 16);
}
else if (topScrollOffset > 1) {
MoveTo(win->portRect.right - 15, 0);
LineTo(win->portRect.right - 15, topScrollOffset - 2);
}
if ( !(features & hasHScroll) ) {
MoveTo(0,win->portRect.bottom - 15);
LineTo(win->portRect.right - 16, win->portRect.bottom - 15);
}
else if ( leftScrollOffset > 1 ) {
MoveTo(0,win->portRect.bottom - 15);
LineTo(leftScrollOffset - 2, win->portRect.bottom - 15);
};
PenSize(savePen.h,savePen.v);
ForeColor(saveColor);
};
badRect = (*(win->visRgn))->rgnBBox;
doRedraw(&badRect);
EndUpdate(win);
SetPort(savePort);
}
void xWindow::doActivate(int active) {
GrafPtr savePort;
Rect R;
if (active) {
if ( features & hasVScroll )
ShowControl(vScroll);
if ( features & hasHScroll )
ShowControl(hScroll);
}
else {
if ( features & hasVScroll )
HideControl(vScroll);
if ( features & hasHScroll )
HideControl(hScroll);
};
if (hasGrow & features) {
R = theWindow->portRect;
R.left = R.right - 14;
R.top = R.bottom - 14;
GetPort(&savePort);
SetPort(theWindow);
if (active)
InvalRect(&R);
else
EraseRect(&R);
SetPort(savePort);
};
}
void xWindow::adjustToNewSize(void) {
Rect bounds;
short height,width;
WindowPtr win;
win = theWindow;
if (features & hasHScroll) {
bounds = win->portRect;
bounds.top = bounds.bottom - 15;
bounds.bottom++;
bounds.left--;
if (features & hasGrow)
bounds.right -= 14;
else
bounds.right++;
bounds.left += leftScrollOffset;
bounds.right -= rightScrollOffset;
if (bounds.right <= bounds.left)
bounds.right = bounds.left + 5;
height = bounds.bottom-bounds.top;
width = bounds.right-bounds.left;
MoveControl(hScroll,bounds.left,bounds.top);
SizeControl(hScroll,width,height);
ShowControl(hScroll);
};
if (features & hasVScroll) {
bounds = win->portRect;
bounds.left = bounds.right - 15;
bounds.right++;
bounds.top--;
if ((features & hasGrow) || (features & hasHScroll))
bounds.bottom -= 14;
else
bounds.bottom++;
bounds.top += topScrollOffset;
bounds.bottom -= bottomScrollOffset;
if (bounds.bottom <= bounds.top)
bounds.bottom = bounds.top + 5;
height = bounds.bottom-bounds.top;
width = bounds.right-bounds.left;
MoveControl(vScroll,bounds.left,bounds.top);
SizeControl(vScroll,width,height);
ShowControl(vScroll);
};
}
void xWindow::ForceRedraw(Rect *badRect) {
GrafPtr savePort;
GetPort(&savePort);
SetPort(theWindow);
if (!badRect || EmptyRect(badRect))
InvalRect(&theWindow->portRect);
else
InvalRect(badRect);
SetPort(savePort);
}
void xWindow::GetTitle(Str255 name) {
GetWTitle(theWindow,name);
}
void xWindow::SetTitle(Str255 name) {
SetWTitle(theWindow,name);
}
int xWindow::GetHVal(void) {
return (features & hasHScroll)? GetCtlValue(hScroll) : 0;
}
int xWindow::GetVVal(void) {
return (features & hasVScroll)? GetCtlValue(vScroll) : 0;
}
void xWindow::SetHVal(int val) {
short oldVal;
short dh;
if (features & hasHScroll) {
oldVal = GetCtlValue(hScroll);
SetCtlValue(hScroll,val);
dh = GetCtlValue(hScroll) - oldVal;
if (dh) doHScroll(dh);
}
}
void xWindow::SetVVal(int val) {
short oldVal;
short dv;
if (features & hasVScroll) {
oldVal = GetCtlValue(vScroll);
SetCtlValue(vScroll,val);
dv = GetCtlValue(vScroll) - oldVal;
if (dv) doHScroll(dv);
}
}
void xWindow::SetHLinesPerPage(short hLines) {
if (hLines > 0)
hLinesPerPage = hLines;
}
void xWindow::SetVLinesPerPage(short vLines) {
if (vLines > 0)
vLinesPerPage = vLines;
}
int xWindow::GetVMax(void) {
return (hasVScroll & features)? GetCtlMax(vScroll) : 0;
}
int xWindow::GetHMax(void) {
return (hasHScroll & features)? GetCtlMax(hScroll) : 0;
}
void xWindow::SetHMax(int max) {
if (max >= 0 && (features & hasHScroll))
SetCtlMax(hScroll,max);
}
void xWindow::SetVMax(int max) {
if (max >= 0 && (features & hasVScroll))
SetCtlMax(vScroll,max);
}